package com.financing.platform.web.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.dto.SalesPrizesDTO;
import com.financing.platform.service.ISalesPrizesService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.DateUtil;

@Controller("salesPrizesController")
@RequestMapping("/api/salesPrizes")
public class SalesPrizesController extends BaseAction  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private ISalesPrizesService salesPrizesService;
	
	@RequestMapping("/listSalesPrizes.shtml")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, SalesPrizesDTO salesPrizes,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query salesPrizes list begin .......");
			
			salesPrizes.setBegin(pager.getStartIndex());
			salesPrizes.setPageSize(pager.getPageSize());
			salesPrizes.setDeleteTag("1");
			int count = salesPrizesService.queryListCount(salesPrizes);
			
			pager.setTotalCount(count);
			
			List<SalesPrizesDTO> salesPrizesList = null;
			
			if(count > 0){
				salesPrizesList = salesPrizesService.queryList(salesPrizes);
			}else{
				salesPrizesList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", salesPrizesList);
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query salesPrizes list end .......");
		}catch(Exception e){
			log.error("query salesPrizes error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/addSalesPrizes.shtml")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, SalesPrizesDTO salesPrizes){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add salesPrizes  begin .......");
			
			DTOdateSet.setCreateInfo(salesPrizes,request.getSession());
			DTOdateSet.setUpdateInfo(salesPrizes,request.getSession());
			
			int id = salesPrizesService.insert(salesPrizes);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add salesPrizes list end .......");
		}catch(Exception e){
			log.error("add salesPrizes error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/addBatchSalesPrizes.shtml")
	@ResponseBody
	public ResultMap addBatchSalesPrizes(HttpServletRequest request){
		ResultMap result = new ResultMap();
		
		try{
			logger.info(" addBatchSalesPrizes  begin .......");
			
			String sales=request.getParameter("salesPrizes");
			
			if(StringUtils.isBlank(sales)){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
				String[] salesPrizes = sales.split("\n");

				//修改之前所有的记录为2 删除
				if(salesPrizes.length>0){
					SalesPrizesDTO s=new SalesPrizesDTO();
					s.setDeleteTag("2");
					salesPrizesService.update(s);
				}
				
				for (int i = 0; i < salesPrizes.length; i++) {
					String saleDto = salesPrizes[i];
					if(StringUtils.isNotBlank(saleDto)){
						String[] d = saleDto.trim().split(",");
						if(d.length<2||!d[1].trim().matches("\\d+")){
							continue;
						}
						int count = Integer.parseInt(d[1].trim());
						for (int j = 0; j < count; j++) {
							SalesPrizesDTO dto=new SalesPrizesDTO();
							dto.setPrizesType(d[0]);
							if(d.length>2){
								dto.setPrizesName(d[2].trim());
							}
							dto.setPrizesCount(1);
							DTOdateSet.setCreateInfo(dto,request.getSession());
							DTOdateSet.setUpdateInfo(dto,request.getSession());
							dto.setPrizesMonth(DateUtil.getDate("yyyyMM"));
							dto.setDeleteTag("1");
							salesPrizesService.insert(dto);
						}
					}
				}
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			logger.info("addBatchSalesPrizes list end .......");
		}catch(Exception e){
			log.error("addBatchSalesPrizes error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailSalesPrizes.shtml")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, SalesPrizesDTO salesPrizes){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail salesPrizes  begin .......");
			
			Integer id = salesPrizes.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			SalesPrizesDTO dto = salesPrizesService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail salesPrizes  end .......");
		}catch(Exception e){
			log.error("query detail salesPrizes error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteSalesPrizes.shtml")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, SalesPrizesDTO salesPrizes){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete salesPrizes  begin .......");
			
			Integer id = salesPrizes.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			salesPrizesService.delete(id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete salesPrizes  end .......");
		}catch(Exception e){
			log.error("delete salesPrizes error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateSalesPrizes.shtml")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, SalesPrizesDTO salesPrizes){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update salesPrizes  begin .......");
			
			Integer id = salesPrizes.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSet.setUpdateInfo(salesPrizes,request.getSession());
			
			salesPrizesService.update(salesPrizes);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update salesPrizes list end .......");
		}catch(Exception e){
			log.error("update salesPrizes error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceSalesPrizes.shtml")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, SalesPrizesDTO salesPrizes){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce salesPrizes  begin .......");
			
			Integer id = salesPrizes.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSet.setUpdateInfo(salesPrizes,request.getSession());
			
			salesPrizesService.updateForce(salesPrizes);
			
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce salesPrizes list end .......");
		}catch(Exception e){
			log.error("updateForce salesPrizes error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
}
